package com.jjw.springbootcvs.controller;

import com.github.pagehelper.PageInfo;

import com.jjw.springbootcvs.config.BootCvsProperties;
import com.jjw.springbootcvs.pojo.dto.SupplierDTO;
import com.jjw.springbootcvs.pojo.entity.Supplier;
import com.jjw.springbootcvs.pojo.entity.SysUser;
import com.jjw.springbootcvs.service.SupplierService;
import com.jjw.springbootcvs.utils.Result;
import jakarta.servlet.http.HttpSession;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author xiaojuan
 * @Date 2024/11/2
 * @Description
 */
@Controller
@RequestMapping("/sys/supplier/")
@RequiredArgsConstructor
public class SupplierController {


    private final SupplierService supplierService;

    private final BootCvsProperties bootCvsProperties;

    @GetMapping("/simpleList")
    @ResponseBody
    public Result simpleList(){
        List<Supplier> supplierList = supplierService.getSupplierList();
        return Result.success(supplierList);
    }

    /**
     * 查询供应商列表
     * @param supplierDTO
     * @param model
     * @return
     */
    @GetMapping("/list")
    public String list(SupplierDTO supplierDTO, Model model){
        PageInfo<Supplier> supplierList = supplierService.getAllSupplierListPage(supplierDTO);
        model.addAttribute("supplierList",supplierList.getList());
        model.addAttribute("totalCount",supplierList.getTotal());
        model.addAttribute("currentPageNo",supplierList.getPageNum());
        model.addAttribute("totalPageCount",supplierList.getPages());

        return "supplier/list";
    }

    /**
     * 新增供应商
     * @param supplier
     * @param session
     * @return
     */
    @PostMapping("/add")
    public String add(Supplier supplier, HttpSession session){
        SysUser user = (SysUser) session.getAttribute(bootCvsProperties.getSessionKey());
        supplier.setCreatedUserId(user.getId());
        supplier.setUpdatedUserId(user.getId());
        supplierService.add(supplier);
        return "redirect:/sys/supplier/list";
    }

    /**
     * 根据供应商的信息查询信息
     * @param id
     * @param model
     * @return
     */
    @GetMapping("/toUpdate/{id}")
    public String update(@PathVariable("id") Long id, Model model){
        Supplier supplier = supplierService.getSupplierById(id);
        model.addAttribute("supplier",supplier);
        return "supplier/update";
    }

    /**
     * 修改供应商信息
     * @param supplier
     * @param session
     * @return
     */
    @PostMapping("/update")
    public String update(Supplier supplier, HttpSession session){
        SysUser user = (SysUser) session.getAttribute(bootCvsProperties.getSessionKey());
        supplier.setUpdatedUserId(user.getId());
        supplierService.update(supplier);
        return "redirect:/sys/supplier/list";
    }

    /**
     * 查询详细信息
     * @param id
     * @param model
     * @return
     */
    @GetMapping("/view/{id}")
    public String view(@PathVariable("id") Long id, Model model){
        Supplier supplier = supplierService.getSupplierById(id);
        model.addAttribute("supplier",supplier);
        return "supplier/view";
    }
    @DeleteMapping("/del/{id}")
    @ResponseBody
    public Result delete(@PathVariable("id") Long id){
        supplierService.delete(id);
        return Result.success();
    }


}
